<?php
namespace backend\controllers;
use Yii;
use yii\web\UploadedFile;
use yii\web\Controller;
use common\Session;
use backend\models\Article;
use backend\models\Articlect;
/**
 * Created by PhpStorm.
 * User: 张
 * Date: 2015/9/23
 * Time: 20:21
 * 文章编辑控制器,添加新文章，更改文章
 */
class EditorController extends Controller{
    public function init(){
        if(!Session::getSession()==101)
        {
            $this->redirect(['/login']);
        }
    }
    public function actionIndex()
    {
        $category=Articlect::find()->where(['level'=>2])->asArray()->all();
        foreach($category as $k=>$v){
            $option[$v['cid']]=$v['name'];
        }
        $info=array(
            'rating'=>'添加文章', 'define'=>'文章标题/文章类型是必填选项。'
        );
        $model=new Article();
        if($model->load(Yii::$app->request->post()) && $model->validate())
        {
            //根据日期生成上传文件的目录并设置权限777,并完善数据验证
            $model->create_time=time();
            /*首页图片上传*/
            $model->thumbnail = UploadedFile::getInstance($model, 'thumbnail');//获取文件上传信息
            if(isset($model->thumbnail)){
                $model->thumbnail->saveAs('uploads/' . $model->thumbnail->baseName . '.' . $model->thumbnail->extension);//保存文件
                $model->thumbnail='uploads/'.$model->thumbnail->name;//把文件目录写入数据库
            }
            //设置文章发布时间
            $model->create_time=time();
            $model->content=$_POST['content'];
            if($model->save()){
                $this->redirect(['article/index']);
            }
        }
        return $this->render('index',['model'=>$model,'info'=>$info,'option'=>$option]);
    }
    public function actionUpdate(){
        $category=Articlect::find()->where(['level'=>2])->asArray()->all();
        foreach($category as $k=>$v){
            $option[$v['cid']]=$v['name'];
        }
        $model=new Article();
        $data='';
        $trash=isset($_GET['trash'])? $_GET['trash']: 0;
        $url=isset($_POST['url'])? $_POST['url']: '';
        if(isset($_GET['id'])){
            $data=Article::find()->where(['id'=>$_GET['id']])->asArray()->one();
        }
        if($model->load(Yii::$app->request->post()) && $model->validate())
        {
            $new=Article::find()->where(['id'=>$_GET['id']])->one();
            $new->title=$model->title;
            $new->category=$model->category;
            $new->content=$_POST['content'];
            $new->status=$model->status;
            $new->thumbnail = UploadedFile::getInstance($new, 'thumbnail');//获取文件上传信息
            if(isset($new->thumbnail)){
                $new->create_time=time();
                $new->thumbnail->saveAs('uploads/' . $new->thumbnail->baseName . '.' . $new->thumbnail->extension);//保存文件
                $new->thumbnail='uploads/'.$new->thumbnail->name;//把文件目录写入数据库
            }else{
                $new->thumbnail=$data['thumbnail'];
            }
            //设置文章修改时间
            $new->modify_time=time();

            if($new->save()){
                $this->redirect($url);
                //$this->redirect(['/article',]);
            }
        }
        $page=$_GET['page'];
        $info=array(
            'rating'=>'文章修改',
            'define'=>'文章已经修改不可恢复，请慎重操作！'
        );
        return $this->render('update',['model'=>$model,'data'=>$data,'page'=>$page,'info'=>$info,'trash'=>$trash,'option'=>$option]);
    }
}
?>
